<?php

namespace App\Jobs\User;

use App\Models\Collecter\MiniCollecterUserMap;
use App\Models\User\WxUser;
use App\Models\Voter\WxVoter;
use App\Models\Voter\WxVoterPlayer;
use App\Models\Voter\WxVoterPlayerBallot;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UserDeleteAfterJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $user_id = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id)
    {
        //
        $this->user_id = $user_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->user_id)){
            $delete_user = WxUser::onlyTrashed()->pluck('id')->toArray();
            if($delete_user){
                // 1、删除投票记录
                WxVoterPlayerBallot::whereIn('user_id', $delete_user)->delete();
                WxVoterPlayer::whereIn('user_id', $delete_user)->delete();
                WxVoter::whereIn('user_id', $delete_user)->delete();
                MiniCollecterUserMap::whereIn('user_id', $delete_user)->delete();
            }
        }else{
            $user = WxUser::onlyTrashed()->find($this->user_id);
            if($user){
                WxVoterPlayerBallot::where('user_id', $user->id)->delete();
                WxVoterPlayer::where('user_id', $user->id)->delete();
                WxVoter::where('user_id', $user->id)->delete();
                MiniCollecterUserMap::where('user_id', $user->id)->delete();
            }
        }
    }
}
